package com.blog.cmrpersonalblog.task;

import com.blog.cmrpersonalblog.service.CommentImageService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 评论图片清理定时任务
 */
@Slf4j
@Component
public class CommentImageCleanupTask {

   @Resource
    private CommentImageService commentImageService;

    /**
     * 清理过期临时图片
     * 每小时执行一次
     */
    @Scheduled(cron = "0 0 * * * ?")
    public void cleanExpiredTempImages() {
        log.info("开始清理过期临时图片...");
        
        try {
            int cleanedCount = commentImageService.cleanExpiredTempImages();
            if (cleanedCount > 0) {
                log.info("清理过期临时图片完成: 清理数量={}", cleanedCount);
            } else {
                log.debug("没有过期临时图片需要清理");
            }
        } catch (Exception e) {
            log.error("清理过期临时图片失败", e);
        }
    }
}
